#include "../Require/Init.iss"
function main()
{
	extension -require isxeq2
	EA "Loading" "Fight.iss"
	declare FightGUID uint local
	declare MeleeAggro uint global 0
	declare RangedAggro uint global 0
	declare combatloops uint local 0
	declare nextaggro uint local 0
	nextaggro:Set[${Script.ExecuteAtom[CheckAggro]}]
	while ${NextTarget} != 0 || ${nextaggro} != 0
	{
		if ${nextaggro} != 0
		{
			EA "Aggro Detected" "${Actor[${nextaggro}].ID} : ${Actor[${nextaggro}].Name}"
			FightGUID:Set[${nextaggro}]
		}
		else
		{
			if ${NextTarget} != 0 && ${Target.ID} != ${NextTarget}
			{
				FightGUID:Set[${NextTarget}]
			}
		}
		wait 2
		Target "${FightGUID}"
		wait 5

		if ${Target(exists)} && ${Target.Health} > 0 && !${Target.IsLocked}
		{
			VirtualState:Set[FIGHT]
			EA "Fighting" "${Target.Name}"
		
			if ${Actor[${FightGUID}].Distance} <= 24.000000
			{
				call MoveToTargetMelee
			}
			else
			{
				call MoveToTargetRanged
			}
			if ${Me.InCombat}
			{
				if !${Actor[${FightGUID}].Target.ID} == ${Me.ID}
				{
					EA "Already Tapped" "${Actor[${FightGUID}].Name} by ${Actor[${FightGUID}].Target.Name}"
				}
			}
			else
			{
				EA "Engage" "${Actor[${FightGUID}].Name}"
				call Engage
				wait 5
			}
			while ${Actor[${FightGUID}](exists)} && ${Actor[${FightGUID}].Health} > 0 
			{
				combatloops:Inc
				if ${combatloops} == 5 && ${Actor[${FightGUID}].Health} == 100 && ${Math.Calc[${Me.Health} / ${Me.MaxHealth} * 100]} == 100
				{
					call MoveToTargetMelee
				}
				elseif ${combatloops} > 3000 && ${Actor[${FightGUID}].Health} == 100
				{
					EA "Blacklisting" "${Actor[${FightGUID}].ID} : ${Actor[${FightGUID}].Name}"
					blacklist:Set["${Actor[${FightGUID}].ID}","${Actor[${FightGUID}].Name}"]
					call ClearTarget
					NextTarget:Set[0]
					Script:End
				}
				if !${Target(exists)} || ${Target.ID} != ${FightGUID}
				{
					EA "ReTargeting" "${Actor[${FightGUID}].Name}"
					Target "${FightGUID}"
				}

				call FaceTarget
	
				if ${Actor[${FightGUID}].IsSwimming}
				{
					call MoveToTargetMelee
				}
				
				if ${Actor[${FightGUID}].Distance} <= 24.000000
				{
					call MoveToTargetMelee
					if ${Actor[${FightGUID}].Distance} <= 7.000000
					{
						call StopMoving
						call Melee
						call Attack
					}
				}
				else
				{
					call MoveToTargetRanged
				}
				waitframe
			}
			EA "End Fighting" "${Actor[${FightGUID}].Name}"
			call ClearTarget
			if ${FightGUID} == ${NextTarget}
			{
				NextTarget:Set[0]
			}
		}
		nextaggro:Set[${Script.ExecuteAtom[CheckAggro]}]
	}
}
function atexit()
{
	VirtualState:Set[ROAM]
	EA "Unloading" "Fight.iss"
}
atom(script):int CheckAggro()
{
	declare a uint local 1
	EQ2:CreateCustomActorArray[byDist,5]
	while ${a:Inc} <= ${EQ2.CustomActorArraySize}
	{
		if ${Actor[${CustomActor[${a}]}](exists)} && ${Actor[${CustomActor[${a}].ID}].Target.ID} == ${Me.ID}
		{
			RETURN ${CustomActor[${a}].ID}
		}
	}
	RETURN 0
}
